﻿using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;

namespace AnlianCommon.API.Commands.SheepFarmes
{
    public class SheepFarmsCreateHandler : IRequestHandler<SheepFarmsCreateCommand, Result>
    {
        private readonly IBaseRepository<SheepFarm> _sheepFarmRepository;
        private readonly IMapper _mapper;

        public SheepFarmsCreateHandler(IBaseRepository<SheepFarm> sheepFarmRepository, IMapper mapper)
        {
            _sheepFarmRepository = sheepFarmRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(SheepFarmsCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code=(int)CommonErrorCode.Added};
            try
            {
                SheepFarm sheepFarm = _mapper.Map<SheepFarm>(request);
                var res = await _sheepFarmRepository.Add(sheepFarm);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Code = (int)CommonErrorCode.Added;
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
